package com.example.administrator.ygddriver.set.safety;

import android.content.Intent;
import android.os.CountDownTimer;
import android.support.v7.widget.CardView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.administrator.ygddriver.R;
import com.example.administrator.ygddriver.base.BaseActivity;
import com.example.administrator.ygddriver.base.BaseBean;
import com.example.administrator.ygddriver.base.BaseHint;
import com.example.administrator.ygddriver.base.UserBean;
import com.example.administrator.ygddriver.into.register.forget.ForGetRequestBean;
import com.example.administrator.ygddriver.liteorm.LiteOrmTools;
import com.example.administrator.ygddriver.set.safety.setpassword.SetPasswordActivity;
import com.example.administrator.ygddriver.tool.OkGoRequests;
import com.example.administrator.ygddriver.tool.SimpleToolbar;
import com.example.administrator.ygddriver.tool.UrlValuse;

import java.util.HashMap;

public class SetValidationActivity extends BaseActivity implements View.OnClickListener {
    private SimpleToolbar simpleToolbar;
    private CardView mNextCv;
    private EditText mEt;
    private Button mBtn;
    private TextView phoneTv;
    private CountDownTimer timer;
    private String phone;

    @Override
    public int initLayout() {
        return R.layout.activity_set_validation;
    }

    @Override
    public void initView() {
        simpleToolbar = findViewById(R.id.activity_set_validation_stb);
        mNextCv = findViewById(R.id.activity_set_validation_cv_next);
        mBtn = findViewById(R.id.activity_set_validation_btn);
        mEt = findViewById(R.id.activity_set_validation_et);
        phoneTv = findViewById(R.id.activity_set_validation_phone_num_tv);
        mBtn.setOnClickListener(this);
        mNextCv.setOnClickListener(this);
    }

    @Override
    public void initDate() {
        setSimpleToolbar();
        LiteOrmTools.getInformation(UserBean.class, this, new LiteOrmTools.On<UserBean>() {
            @Override
            public void hasData(UserBean data) {
                phone = data.getC_account();
                phoneTv.setText("设置密码需要验证你的手机号" + data.getC_account());
            }
        }, new LiteOrmTools.OFF() {
            @Override
            public void noData() {

            }
        });

    }

    private void setSimpleToolbar() {
        simpleToolbar.setMainTitle("账号认证");
        simpleToolbar.setMainTitleColor("#000000");
        simpleToolbar.setBackIv(R.mipmap.u126);
        simpleToolbar.setToolColor("#ffffff");
        simpleToolbar.setBackIvClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.activity_set_validation_cv_next:
                if (!TextUtils.isEmpty(mEt.getText().toString()) && mEt.getText().toString().length() == 6) {
                    initcheckForgetPwdAuthCodeHtml(phone, mEt.getText().toString());
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(SetValidationActivity.this, "提示", "请检查验证码是否正确");
                    baseHint.show();
                }

                break;

            case R.id.activity_set_validation_btn:
                startTime();
                HashMap<String, String> params = new HashMap<>();
                params.put("c_phone", phone);
                params.put("type", "6");
                initGetVerificationHtml(params);
                break;
        }
    }

    //获取验证码
    private void initGetVerificationHtml(HashMap<String, String> params) {
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE + UrlValuse.CREATE_VERIFICATION_CODE, params, BaseBean.class, new OkGoRequests.OnTrue<BaseBean>() {
            @Override
            public void hasData(BaseBean data) {
                if (data.isSuccess()) {

                } else {
                    timer.cancel();
                    mBtn.setClickable(true);
                    mBtn.setText("获取验证码");
                    BaseHint baseHint = new BaseHint.addHint().setParameter(SetValidationActivity.this, "提示", data.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<BaseBean>() {
            @Override
            public void noData() {

            }
        });


    }

    private void startTime() {
        timer = new CountDownTimer(60000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mBtn.setClickable(false);
                mBtn.setText((millisUntilFinished / 1000) + "秒");
            }

            @Override
            public void onFinish() {
                mBtn.setClickable(true);
                mBtn.setText("获取验证码");
            }
        }.start();


    }

    //忘记密码 验证手机号及验证码
    private void initcheckForgetPwdAuthCodeHtml(final String phone, String code) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("c_account", phone);
        hashMap.put("c_code", code);
        hashMap.put("c_type", "1");
        OkGoRequests.getInstance().putRequestAsync(UrlValuse.POST_API_BASE + UrlValuse.CHECK_FORGET_PW_AUTHCODE, hashMap, ForGetRequestBean.class, new OkGoRequests.OnTrue<ForGetRequestBean>() {
            @Override
            public void hasData(ForGetRequestBean date) {
                if (date.isSuccess()) {
                    Intent intent = new Intent(SetValidationActivity.this, SetPasswordActivity.class);
                    intent.putExtra("phone", phone);
                    startActivity(intent);
                    finish();
                } else {
                    BaseHint baseHint = new BaseHint.addHint().setParameter(SetValidationActivity.this, "提示", date.getLocalizedMessage());
                    baseHint.show();
                }

            }
        }, new OkGoRequests.OnError<ForGetRequestBean>() {
            @Override
            public void noData() {

            }
        });

    }
}
